package lib.bone.admin.event;

import lib.bone.admin.controller.nepu.NepuWorkloadController;
import lib.bone.audit.workload.domain.NepuWorkload;
import lib.bone.audit.workload.service.NepuWorkloadService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.flow.constant.FlowWorkStatusConst;
import lib.bone.flow.domain.entity.FlowWork;
import lib.bone.flow.event.FlowWorkRemoveEvent;
import lib.bone.flow.service.FlowWorkService;
import lib.bone.nepudata.service.ResearchServiceCtl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

/**
 * @author kindear
 */
@Primary
@Slf4j
@Service
public class FlowWorkRemoveEventImpl implements FlowWorkRemoveEvent {

    @Autowired
    NepuWorkloadController workloadCtl;

    @Autowired
    FlowWorkService flowWorkService;

    @Autowired
    NepuWorkloadService workloadService;

    @Autowired
    ResearchServiceCtl researchServiceCtl;

    @Override
    @Transactional
    public void process(String flowWorkId) {
        // 查询流程实例信息
        Optional<FlowWork> flowWorkOp = flowWorkService.getById(flowWorkId);
        if (!flowWorkOp.isPresent()) throw new BusinessException(ResponseCodeConst.NOT_FOUND);
        FlowWork flowWork = flowWorkOp.get();
        if (flowWork.getFlowWorkStatus().equals(FlowWorkStatusConst.FINISHED)){
            // 不删除
            throw new BusinessException(ResponseCodeConst.FORBIDDEN,"已完成流程无法删除");
        }
        else {
            researchServiceCtl.deleteById(flowWork.getServiceId(),flowWork.getServiceType());
        }
        // 查询关联工作量信息
//        try {
//            Optional<NepuWorkload> workloadOp = workloadService.getLinkEntity(flowWork.getServiceId());
//            if (workloadOp.isPresent()) {
//                NepuWorkload workload = workloadOp.get();
//                if (workload.getWorkloadLock()==1) {
//                    // 解锁工作量
//                    ResponseDTO<Object> responseDTO = workloadCtl.unlockWorkload(workload.getId());
//                    // 解锁失败 -- 无法删除
//                    if (responseDTO.getSuccess().equals(false)) {
//                        throw new BusinessException(ResponseCodeConst.FORBIDDEN, responseDTO.getMsg());
//                    }
//                }
//                // 1. 首先删除工作量
//                workloadService.deleteById(workload.getId());
//            }
//        }
//        catch (Exception e){
//            e.printStackTrace();
//        }
//        finally {
//            // 2. 其次删除关联实体信息
//            log.info("开始删除关联实体");
//            researchServiceCtl.deleteById(flowWork.getServiceId(),flowWork.getServiceType());
//            // 3. 执行完毕
//            log.info("ID:【{}】流程实例删除-触发事件执行成功",flowWork.getId());
//        }
        // 查询关联工作量信息
//        Optional<NepuWorkload> workloadOp = workloadService.getLinkEntity(flowWork.getServiceId());
//        if (!workloadOp.isPresent()) throw new BusinessException(ResponseCodeConst.NOT_FOUND,"工作量记录不存在");
//        NepuWorkload workload = workloadOp.get();
//        // 解锁工作量
//        ResponseDTO<Object> responseDTO = workloadCtl.unlockWorkload(workload.getId());
//        // 解锁失败 -- 无法删除
//        if (responseDTO.getSuccess().equals(false)){
//            throw new BusinessException(ResponseCodeConst.FORBIDDEN,responseDTO.getMsg());
//        }
//        // 1. 首先删除工作量
//        workloadService.deleteById(workload.getId());
//        // 2. 其次删除关联实体信息
//        researchServiceCtl.deleteById(flowWork.getServiceId(),flowWork.getServiceType());
//        // 3. 执行完毕
//        log.info("ID:【{}】流程实例删除-触发事件执行成功",flowWork.getId());
    }
}
